home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 27 / CU Amiga Magazine's Super CD-ROM 27 (1998)(EMAP Images)(GB)[!][issue 1998-10].iso / CUCD / Programming / JForth / JTools / Demos / demo_interrupt < prev    next >
Encoding:
Text File  |  1991-08-14  |  2.0 KB  |  89 lines

  1. \ Interrupt based 60 Hz timer.
  2. \
  3. \ This demonstrates how to use a Vertical Blanking
  4. \ Interrupt Server to increment a variable every
  5. \ 1/60th of a second.
  6. \
  7. \ Translated from 'C' by Phil Burk
  8. \
  9. \ Warning - Interupts can be very tricky. Proceed with caution
  10. \ and don't get discouraged.  Be careful about what you do
  11. \ inside an interrupt server.  Don't change things that can
  12. \ mess up other code.
  13.  
  14. include? interrupt ji:exec/interrupts.j
  15. include? INTB_VERTB ji:hardware/intbits.j
  16.  
  17. ANEW TASK-INT_TIME
  18. decimal
  19. variable VERTBINTR
  20. variable TIME-COUNT
  21.  
  22. : ADDINTSERVER() ( type interrupt -- )
  23.     >abs
  24.     call exec_lib addintserver drop
  25. ;
  26.  
  27. : REMINTSERVER() ( type interrupt -- )
  28.     >abs
  29.     call exec_lib remintserver drop
  30. ;
  31.  
  32. ASM TIME.INT.SERVER ( -- , called when vertical blanking occurs )
  33. \ The manual implies that A1 points to the is_data member.
  34. \ I found through experimentation that A1 actually contains
  35. \ the contents of is_data.
  36.     addq.l    #1,(a1)       ( increment counter )
  37.     moveq.l   #0,d0         ( continue chain )
  38.     rts
  39. END-CODE
  40.  
  41. : TIME.INT.INIT  ( -- , setup interrupt)
  42.     0 time-count !
  43.     vertbintr @ 0=  ( make sure not done twice )
  44.     IF
  45.         MEMF_PUBLIC sizeof() interrupt allocblock ?dup
  46.         IF
  47.             dup>r vertbintr !  ( save for TERM )
  48. \ Set values in structure.
  49.             NT_INTERRUPT r@ .. is_node ..! ln_type
  50.             -60  r@ .. is_node ..! ln_pri
  51.             0" VertB Timer" >abs r@ .. is_node ..! ln_name
  52.             time-count >abs r@ ..! is_data
  53.             ' time.int.server >abs r@ ..! is_code
  54. \
  55. \ Add to EXEC List of Interrupt Servers for VERTB.
  56.             INTB_VERTB r> addintserver()
  57.         ELSE
  58.             ." TIME.INT.INIT - Not enough space for timer interrupt!" cr
  59.             abort
  60.         THEN
  61.     THEN
  62. ;
  63.  
  64. : TIME.INT.TERM ( -- , remove and free timer interrupt )
  65.     vertbintr @ ?dup
  66.     IF  INTB_VERTB over remintserver()
  67.         freeblock
  68.         0 vertbintr !
  69.     THEN
  70. ;
  71.  
  72. : TIME@  ( -- , current_time )
  73.     time-count @
  74. ;
  75.  
  76. : WATCH ( -- , watch timer increment )
  77.     time.int.init
  78.     BEGIN
  79.         time@ . cr
  80.         ?terminal
  81.     UNTIL
  82.     time.int.term
  83. ;
  84.  
  85.  
  86. cr
  87. ." Enter:  WATCH    to see counter being incremented!" cr
  88. ." Then hit a key to stop." cr
  89.